package com.zxy.controlcenter.core.datasource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author zxy
 */
@Component
@Slf4j
@Aspect
@Order(value = 1)
public class DataSourceAspect {

    @Before("@annotation(targetDataSource)")
    public void doBefore(TargetDataSource targetDataSource) {
        log.info("选择数据源:{}",targetDataSource.value().getValue());
        DataSourceContextHolder.setDataSource(targetDataSource.value());
    }

    @After("@annotation(targetDataSource)")
    public void doAfter(JoinPoint point, TargetDataSource targetDataSource) {
        log.debug("Revert DataSource : {} > {}", targetDataSource.value(), point.getSignature());
        DataSourceContextHolder.clear();
    }
}
